package com.wang.util;

import com.wang.conf.AppData;
import com.wang.pojo.Device;
import com.wang.service.DeviceService;
import com.wang.service.DeviceServiceImpl;

import javax.swing.*;
import java.text.ParseException;
import java.util.Date;

/**
 * @author wcy
 * @date 2022-09-17 11:17
 */
public class DevStatusUtil {

    //处理在线事件
    public static void handerOnlineEvent(Integer devId) throws ParseException {
        DeviceService deviceService = SpringUtil.getBean("deviceServiceImpl", DeviceServiceImpl.class);
        Device device = deviceService.selectDeviceByDevId(devId);
        if (device.getIsBind().equals(1) && device.getDevStatusCode().equals(AppData.offLineCode)) {
            if (EmptyUtil.isNotNull(device.getLastOfflineTime())) {


                Long longTotalOffline = Long.parseLong(device.getTotalOfflineTime());


                longTotalOffline += (new Date().getTime() - DateUtil.stringToDate(device.getLastOfflineTime(), "yyyy-MM-dd HH:mm:ss").getTime());

                ;

                //存储最近在线时间 和 累计离线时间

                String lastOnlineTime = DateUtil.getStrDate();
                String totalOffline = longTotalOffline.toString();
                deviceService.updateDevOnlineData(devId, AppData.onLineCode, lastOnlineTime, totalOffline);

            } else {
                //存储最近在线时间 和 累计离线时间

                String lastOnlineTime = DateUtil.getStrDate();
                String totalOffline = "0";
                deviceService.updateDevOnlineData(devId, AppData.onLineCode, lastOnlineTime, totalOffline);
            }
        } else {

            deviceService.updateDevStatusCode(device.getDevGId(), AppData.onLineCode);
        }
    }

    //处理离线事件
    public static void handerOfflineEvent(Integer devId) throws ParseException {
        DeviceService deviceService = SpringUtil.getBean("deviceServiceImpl", DeviceServiceImpl.class);

        Device device = deviceService.selectDeviceByDevId(devId);



        if (EmptyUtil.isNotNull(device.getIsBind()) && device.getIsBind().equals(1) && device.getDevStatusCode().equals(AppData.onLineCode)) {
            if (EmptyUtil.isNotNull(device.getLastOnlineTime())) {
                Long longTotalOnline = Long.parseLong(device.getTotalOnlineTime());
                longTotalOnline += (new Date().getTime() - DateUtil.stringToDate(device.getLastOnlineTime(), "yyyy-MM-dd HH:mm:ss").getTime());

                //存储最近离线时间 和 累计离线时间

                String lastOffLineTime = DateUtil.getStrDate();
                String totalOnline = longTotalOnline.toString();
                deviceService.updateDevOfflineData(devId, AppData.offLineCode, lastOffLineTime, totalOnline);

            } else {
                //存储最近离线时间 和 累计离线时间
                String lastOffLineTime = DateUtil.getStrDate();
                String totalOffline = "0";
                deviceService.updateDevOfflineData(devId, AppData.offLineCode, lastOffLineTime, totalOffline);
            }
        } else {

            deviceService.updateDevStatusCode(device.getDevGId(), AppData.offLineCode);
        }
    }


}
